package com.hopes.bizz.line.common;

import com.hopes.boss.admin.api.entity.SysDictItem;
import com.hopes.boss.admin.api.feign.RemoteDictService;
import com.hopes.boss.admin.api.resolver.DictLoader;
import com.hopes.boss.admin.api.resolver.EnumResolver;
import com.hopes.boss.common.core.constant.SecurityConstants;
import com.hopes.boss.common.core.util.R;
import com.hopes.boss.common.core.util.StringUtils;
import com.hopes.boss.common.security.annotation.Inner;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author mengran.yu
 * @Description 模块（数据表）字典
 */
@RestController
@AllArgsConstructor
@RequestMapping("/common/dict")
@Tag(description = "label-value", name = "标签-数据接口")
@Slf4j
public class ParkLineDictController {

	private final RemoteDictService remoteDictService;

	/**
	 * 通过字典类型查找字典
	 * @param type 类型
	 * @return 同类型字典
	 */
	@GetMapping("{type}")
	public R<List<SysDictItem>> getDictByType(@PathVariable String type) {
		log.info("标签数据对象：{}", type);
		try {
			final EnumResolver<?> resolver = DictLoader.getInstance().getEnumResolverByType(type);
			if (null != resolver && StringUtils.isBlank(resolver.getMethodName())) {
				return remoteDictService.getDictByType(type, SecurityConstants.FROM_IN);
			}
			return DictLoader.getInstance().listDictByEnumResolver(resolver);
		} catch (Exception e) {
			log.error("[dic]方法调用出错：", e);
			return R.failed(e.getMessage());
		}
	}


	/**
	 * 通过字典类型查找字典
	 * @param type 类型
	 * @return 同类型字典
	 */
	@Inner
	@GetMapping("/{scope}/{type}")
	public R<List<SysDictItem>> listDictByScopeType(@PathVariable String scope, @PathVariable String type) {
		log.info("标签数据对象：{}, 字典项：{}", scope, type);
		try {
			if (StringUtils.isBlank(scope) || StringUtils.isBlank(type)) {
				return R.failed("查询参数不可为空！");
			}
			return DictLoader.getInstance().listDictByScopeType(scope, type);
		} catch (Exception e) {
			log.error("[dic]方法调用出错：", e);
			return R.failed(e.getMessage());
		}
	}
}
